/**
 *
 */
package org.t2.game.server.module.mail.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.gaming.service.repository.IRepository;
import org.gaming.service.repository.Repository;
import org.gaming.tool.GsonUtil;
import org.springframework.stereotype.Component;
import org.t2.game.server.module.mail.model.Mail;

import sh.t2.share.module.mail.MailLog;

/**
 * @author YY
 *
 */
@Component
public class MailLogger {
	
    private IRepository<MailLog> repository;
    protected IRepository<MailLog> repository() {
        if (repository == null) {
            repository = Repository.get(MailLog.class);
        }
        return repository;
    }

    public void logOnDelete(List<Mail> mails) {
    	List<MailLog> logs = new ArrayList<>();
        for (Mail mail : mails) {
        	logs.add(buildLog(mail));
        }
        repository().insertAll(logs);
    }

    private MailLog buildLog(Mail mail) {
        MailLog log = new MailLog();
        log.setOriMailId(mail.getId());
        log.setOwnerId(mail.getReceiver());
        log.setTemplate(mail.getTemplate());
        log.setTitleArgs(GsonUtil.toJson(mail.getTitleArgs()));
        log.setContentArgs(GsonUtil.toJson(mail.getContentArgs()));
        log.setAttachment(mail.getAttachment());
        log.setRead(mail.isRead());
        log.setReceive(mail.isReceive());
        log.setExpireTime(mail.getExpireTime());
        log.setGameCause(mail.getGameCause());
        log.setGameCauseText(mail.getGameCauseText());
        log.setFromPoolId(mail.getFromPoolId());
        log.setTime(LocalDateTime.now());
        return log;
    }
}
